package controller;

import api.TemplateMsgAPI;
import common.pojo.CommonResult;
import common.spring.base.BaseController;
import common.spring.redis.RedisUnits;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.PayInfo;
import pojo.WxUserFormId;
import pojo.WxUserInfo;
import pojo.form.UserSendCountAddForm;
import service.PayInfoService;
import service.UserSendCountService;
import service.WxUserFormIdService;
import service.WxUserInfoService;

import java.util.Date;
import java.util.List;


/**
 * Created by asus on 2018/12/26.
 */
@Controller
@RequestMapping("/sendTemplate")
public class SendTemplateController extends ManagerBaseController{

    private final Logger log = LoggerFactory.getLogger(SendTemplateController.class);
    @Autowired
    TemplateMsgAPI msgAPI;

    @Autowired
    RedisUnits redisUnits;

    @Autowired
    PayInfoService infoService;

    @Autowired
    WxUserInfoService userInfoService;
    @Autowired
    UserSendCountService countService;
    @Autowired
    WxUserFormIdService idService;
    @RequestMapping(value = "/send",method = RequestMethod.POST)
    @ResponseBody
    public CommonResult sendTemplateMsg(
            @RequestParam(value = "orderCode",required = false) String orderCode
    ){
        PayInfo payInfo=infoService.getPayInfo(orderCode);
        if(payInfo==null){
            return CommonResult.returnFailsWrapper("支付信息不存在");
        }
        if(payInfo.getOpenId()!=null && payInfo.getPrepay_id()!=null){
            msgAPI.sendTemplateMsg(payInfo.getOpenId(),payInfo.getPrepay_id());
        }
        log.warn("--SendTemplateController");
        return CommonResult.returnDataWrapper();
    }

    @RequestMapping(value = "/msg",method = RequestMethod.POST)
    @ResponseBody
    public void sendTemplateMsgs(
            @RequestParam(value = "customId",required = false) String customId,
            @RequestParam(value = "shopId",required = false) String shopId,
            @RequestParam(value = "shopName",required = false) String shopName,
            @RequestParam(value = "message",required = false) String message

    ){
        if(StringUtils.isBlank(customId)){
            log.warn("--customId--isEmpty--");
            return;
        }
        if(StringUtils.isBlank(shopId)){
            log.warn("--shopId--isEmpty--");
            return;
        }
        if(StringUtils.isBlank(shopName)){
            log.warn("--shopName--isEmpty--");
            return;
        }
        if(StringUtils.isBlank(message)){
            log.warn("--message--isEmpty--");
            return;
        }
        WxUserInfo wxUserInfo=userInfoService.getWxUserInfo(NumberUtils.toLong(customId));
        if(wxUserInfo==null){
            log.warn("--wxUserInfo--isEmpty--");
            return;
        }
        List<WxUserFormId> list=idService.getWxUserFormIdList(wxUserInfo.getOpenId());
        String formId="";
        if(list.size()>0){
            formId=list.get(0).getFormId();
        }else {
            log.warn("--未查询到用户formId--");
            return;
        }
        Number number = countService.getUserSendCount(wxUserInfo.getOpenId());
        if(number.intValue()<=2){
            countService.addUserSendCount(new UserSendCountAddForm(wxUserInfo.getOpenId(),new Date()));
           msgAPI.sendTemplateLeavingMsg(wxUserInfo.getOpenId(),formId,shopName,message,NumberUtils.toLong(shopId),wxUserInfo.getId());
            idService.deleteWxUserFormId(list.get(0).getId());
        }
    }


}
